package cn.daqisoft.util;

import net.sf.json.JSONObject;

/**
 *
 * @description: 接口返回结果处理工具类
 * @author: dream breeze
 * @date: 2018/11/1 11:27
 */
public class ResultUtil {

	private String code = "";//1：成功 2：失败  3：失败--token失效专用

    private String msg = "";

    private JSONObject obj = new JSONObject();

    public String getMsg() {
        return msg;
    }

    public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public void setMsg(String msg) {
        this.msg = msg;
    }

    public JSONObject getObj() {
        return obj;
    }

    public void setObj(JSONObject obj) {
        this.obj = obj;
    }
    
    
    /**
     * 失败
     * @param msg 失败的消息
     * @return {Object}
     */
    public static ResultUtil renderError(String msg) {
        ResultUtil result = new ResultUtil();
        result.setMsg(msg);
        result.setCode("500");
        return result;
    }
    
    /**
     * 自定义返回，code非1非2
     * @param code
     * @param msg
     * @return
     */
    public static ResultUtil renderOther(String code,String msg,JSONObject obj) {
        ResultUtil result = new ResultUtil();
        result.setCode(code);
        result.setMsg(msg);
        result.setObj(obj);
        return result;
    }
    /**
     * 成功，无返回消息与无返回对象
     * @return {Object}
     */
    public static ResultUtil renderSuccess() {
        ResultUtil result = new ResultUtil();
        result.setCode("200");
        return result;
    }

    /**
     * 成功，有返回消息与无返回对象
     * @param msg 消息
     * @return {Object}
     */
    public static ResultUtil renderSuccess(String msg) {
        ResultUtil result = new ResultUtil();
        result.setCode("200");
        result.setMsg(msg);
        return result;
    }

    /**
     * 成功，无返回消息与有返回对象
     * @param obj 成功时的对象
     * @return {Object}
     */
    public static ResultUtil renderSuccess(JSONObject obj) {
        ResultUtil result = new ResultUtil();
        result.setCode("200");
        if(obj!=null){
        	result.setObj(obj);
        } 
        return result;
    }

    /**
     * 成功，有返回消息与有返回对象
     * @param obj 成功时的对象消息
     * @return {Object}
     */
    public static ResultUtil renderSuccess(String msg, JSONObject obj) {
    	ResultUtil result = new ResultUtil();
        result.setCode("200");
        result.setMsg(msg);
        if(obj!=null){
        	result.setObj(obj);
        } 
        return result;
    }
    
 
}
